﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Text;
using CsvHelper;
using CsvHelper.Configuration;

namespace Importer
{
  public class ExportCsv
  {
    [ImportMany]
    public IEnumerable<ITable> Tables { get; set; }

    public void Run()
    {
      foreach (var table in Tables)
      {
        using (var file = new StreamWriter(table + ".csv", false, Encoding.UTF8))
        using (var writer = new CsvWriter(file, new CsvConfiguration
                                                  {
                                                    Delimiter = ';',
                                                    UseInvariantCulture = true,
                                                  }))
        {
          foreach (var col in table.Columns)
            writer.WriteField(col.ColumnName);
          writer.NextRecord();
          foreach (var row in table.Rows)
          {
            foreach (var value in row.Values)
              writer.WriteField(value);
            writer.NextRecord();
          }
        }
      }
    }
  }
}